package com.suwish.pc.ui.dialog;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import com.android.ddmlib.IDevice;
import com.suwish.pc.adb.AdbManager;
import com.suwish.pc.adb.Command;
import com.suwish.pc.ui.component.UIDialog;
import com.suwish.pc.ui.component.UIAdbTerminal;
import com.suwish.pc.ui.util.DeviceHelper;
import com.suwish.pc.ui.util.UIHelper;
import com.suwish.pc.util.StringUtils;

public class ShellDialog extends UIDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JMenuBar menuBar= null;
	private JMenu connectMenu = null;
	private JMenu toolMenu = null;
	private JMenu helpMenu = null;
	
	private UIAdbTerminal terminalPanel = null;
	
	private Window parent = null;
	private String title = null;
	public ShellDialog(Frame owner, String title){
		super(owner, title);
		parent = owner;
		this.title = title;
		initUI();
		initData();
		initEvent();		
		setSize(new Dimension(600, 500));
		setLocationRelativeTo(owner);
	}
	private void initData(){
		try{
			terminalPanel.addNoticeMessage("Conect device \nMenuBar->Connect->connect");
		}catch(Exception ex){
			ex.toString();
		}
	}
	private void initEvent(){
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				terminalPanel.dispose();
			}
			
		});
	}
	public void appendTitle(String title){
		setTitle(this.title + " " + title);
	}
	private void initUI(){
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		terminalPanel = new UIAdbTerminal(this);
		connectMenu = new JMenu("Connect");
		menuBar.add(connectMenu);
		JMenuItem connectItem = new JMenuItem("connect");
		connectItem.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				DdmsDeviceSelectDialog dialog = new DdmsDeviceSelectDialog(parent);
				dialog.setVisible(true);
				IDevice device = dialog.getSelectedDevice();
				if(device == null) return ;
				if(StringUtils.isEmpty(device.getSerialNumber())){
					UIHelper.showErrorDialog(getInstance(), "Unkonw Device SerialNumber");
					return;
				}				
				try{
					String title  = DeviceHelper.getDeviceDescName(device);
					Process process = AdbManager.execCommand(new Command(new String[]{"-s",device.getSerialNumber(),"shell"}), terminalPanel);
					terminalPanel.dispose();
					terminalPanel.setProcess(process, title);
					appendTitle(title);
				}catch(Exception ex){
					UIHelper.showErrorDialog(getInstance(), ex.getMessage());
				}
			}
		});
		connectMenu.add(connectItem);
		JMenuItem disconnectItem = new JMenuItem("disconnect");
		connectMenu.add(disconnectItem);
		disconnectItem.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				terminalPanel.dispose();				
			}
		});
		connectMenu.addSeparator();
		JMenuItem closeItem = new JMenuItem("close");
		closeItem.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();				
				terminalPanel.dispose();
			}
		});
		connectMenu.add(closeItem);
		add(terminalPanel);
		
		toolMenu = new JMenu("Tool");
		menuBar.add(toolMenu);
		JMenuItem terminalItem = new JMenuItem("Terminal");
		toolMenu.add(terminalItem);
		terminalItem.setEnabled(false);
		terminalItem.addActionListener(terminalPanel.getAction());
		
		helpMenu = new JMenu("Help");
		menuBar.add(helpMenu);
		JMenuItem aboutItem = new JMenuItem("About");
		helpMenu.add(aboutItem);
		aboutItem.addActionListener(new AbstractAction() {
			private static final long serialVersionUID = 1L;
			@Override
			public void actionPerformed(ActionEvent e) {
				String message = "终端，使用ADB命令Shell，无视Preferences中设置的连接模式";				
				UIHelper.showMessageDialog(getInstance(), message, "Message", 1);
				
			}
		});
	}
}
